package com.jianyun.jyzs.adapter;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jianyun.jyzs.R;
import com.jianyun.jyzs.activity.EngDetialActivity;
import com.jianyun.jyzs.bean.EngineeringResult;
import com.jianyun.jyzs.constant.SysConstant;
import com.jianyun.jyzs.utils.DateTime;
import com.jianyun.jyzs.utils.JoinImageUtils;
import com.jianyun.jyzs.widget.JointImage;


import java.lang.reflect.Type;
import java.util.Calendar;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 个人动态条目
 * Created by mlli on 2017/11/11.
 */

public class PdItemAdaper extends RecyclerView.Adapter {
    private Activity context;
    private List<EngineeringResult.EngineerBean> list;
    private final Gson gson;

    public PdItemAdaper(Activity context, List<EngineeringResult.EngineerBean> list) {
        this.context = context;
        this.list = list;
        gson = new Gson();
    }

    public void setList(List<EngineeringResult.EngineerBean> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_personal_dynamic, parent, false);
        return new MHolder(view);
    }

    int quesetISerach=1;
    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {

        final MHolder mHolder = (MHolder) holder;
        final EngineeringResult.EngineerBean bean = list.get(position);
        mHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                context.startActivityForResult(new Intent(context,EngDetialActivity.class).putExtra(SysConstant.msg_key_string1,bean.getId()),111);
            }
        });
        if (position == 0) {
            mHolder.date.setVisibility(View.VISIBLE);
            String dt = bean.getDt();
            Long aLong = DateTime.getmillionTime(dt);
            String md = DateTime.MD(aLong);
            String[] ym = md.split("月");
            Log.i("test","时间；"+ym.toString());
            mHolder.date.setText(ym[1]);
            mHolder.date2.setText(ym[0]+"月");
            if (DateTime.YMD(Calendar.getInstance().getTimeInMillis()).equals(DateTime.YMD(aLong))){
                mHolder.date.setText("今天");
                mHolder.date2.setVisibility(View.GONE);
            }
        } else {
            mHolder.date.setVisibility(View.INVISIBLE);
            mHolder.date2.setVisibility(View.INVISIBLE);
        }
        mHolder.t1.setText(bean.getProjectName());
        mHolder.t2.setText(bean.getTextStr());
        Type attachtype = new TypeToken<List<EngineeringResult.EngineerBean.AttachmentBean>>() {
        }.getType();
        List<EngineeringResult.EngineerBean.AttachmentBean> attachmentlist = gson.fromJson(bean.getListAttachment(), attachtype);
        if (attachmentlist.size() == 0){
            return;
        }else {
            if (quesetISerach==1){
                JoinImageUtils.getBitList(context, attachmentlist, new JoinImageUtils.BitmapListListener() {
                    @Override
                    public void bitListener(List<String> bitmaplist) {
                        //quesetISerach=2;
                        Log.i("test","地址："+bitmaplist);
                        //Glide.with(context).load()
                        Glide.with(context).load(bitmaplist.get(0)).into(mHolder.image);
                        //mHolder.image.setImageLista(bitmaplist);
                    }
                });

            }

        }


    }

    private void setTimeText(TextView date, TextView date2, String time, String dt) {
        if (time != null && !time.equals("")) {
            if (time.equals("今天")) {
                date2.getPaint().setFakeBoldText(true);//加粗
                date.setText("今");
                date2.setText("天");
                date2.setTextSize(26);
            } else if (time.equals("昨天")) {
                date2.getPaint().setFakeBoldText(true);//加粗
                date.setText("昨");
                date2.setText("天");
                date2.setTextSize(26);
            } else {
                date2.getPaint().setFakeBoldText(false);//不加粗
                date.setText(time.split(",")[0]);
                date2.setText(time.split(",")[1] + "月");
                date2.setTextSize(16);
            }
        } else {
            date2.getPaint().setFakeBoldText(false);//不加粗
            date.setText(dt.split("-")[2]);
            date2.setText(dt.split("-")[1] + "月");
        }
    }



    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    class MHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.date)
        TextView date;
        @BindView(R.id.date2)
        TextView date2;
        @BindView(R.id.t1)
        TextView t1;
        @BindView(R.id.t2)
        TextView t2;
//        @BindView(R.id.t3)
//        TextView t3;
        @BindView(R.id.image)
        JointImage image;

        public MHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
